package com.guobox.movies.usecase;

import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.guobox.movies.APP;
import com.guobox.movies.constant.Constant;
import com.guobox.movies.proxy.HttpUrls;
import com.guobox.movies.entity.ApkInfo;
import com.guobox.movies.present.MovieListPresent;
import com.guobox.movies.utils.PhoneInfoUtils;
import com.guobox.movies.utils.httpsutils.DownloadUtil;
import com.guobox.movies.utils.httpsutils.OkhttpsUtils;

import java.io.File;
import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by xuxinlei on 2017/7/10.
 */

public class UpdateModelImp implements UpdateModel{

    private ApkInfo apkInfo = null;

    private MovieListPresent movieListPresent;
    private Handler handler = null;

    public UpdateModelImp(MovieListPresent movieListPresent) {
        this.movieListPresent = movieListPresent;

        handler = new Handler(Looper.getMainLooper());
    }

    @Override
    public void checkAppUpdate() {

        Request request = new Request.Builder()
                .url(HttpUrls.getAppUpdateUrl().build())
                .build();
        OkhttpsUtils.getOkHttpClient().newCall(request)
                .enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        movieListPresent.checkAppUpdateCallBack(false,"");
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String resJson = response.body().string();
                        Gson gson = new Gson();

                        apkInfo = gson.fromJson(resJson, ApkInfo.class);
                        if (apkInfo.getVersionCode() > PhoneInfoUtils.getVerCode(APP.getApplication())) {
                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                                    movieListPresent.checkAppUpdateCallBack(true,apkInfo.getDescription());
                                }
                            });

                        } else {
                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                                    movieListPresent.checkAppUpdateCallBack(false,"");
                                }
                            });

                        }
                    }
                });
    }

    @Override
    public void delApk() {
        File file = new File(Constant.Storage_path + Constant.APPID + ".apk");
        if (file.exists() && file.isFile()) {
            file.delete();
        }
    }
    @Override
    public void downloadApp() {
//        Uri uri = Uri.parse(apkInfo.getAppUrl());
//        Intent downloadIntent = new Intent();
//        downloadIntent.setData(uri);
//        downloadIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//        APP.getApplication().startActivity(downloadIntent);
        if (apkInfo != null) {
            DownloadUtil.get().download(apkInfo.getAppUrl(),Constant.Update_doc,Constant.APPID+".apk", new DownloadUtil.OnDownloadListener() {
                @Override
                public void onDownloadSuccess() {
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            if (!TextUtils.isEmpty(DownloadUtil.downloadPath)) {

                                movieListPresent.downloadAppResult(true);

                            } else {
                                movieListPresent.downloadAppResult(false);
                            }
                        }
                    });



                }
                @Override
                public void onDownloading(final int progress) {
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            movieListPresent.downloadAppProgress(progress);
                        }
                    });

                }
                @Override
                public void onDownloadFailed() {
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            movieListPresent.downloadAppResult(false);
                        }
                    });

                }
            });
        }
    }
}
